package webEquipeRechercheGwt.client.allEntityShowingPanel;

import java.util.ArrayList;
import java.util.Collection;

import webEquipeRechercheGwt.client.callback.consultation.CallBackGetAllSeminar;
import webEquipeRechercheGwt.client.callback.consultation.commands.CommandGetAllSeminarsImplAllSeminarPanel;
import webEquipeRechercheGwt.client.entityMirror.SeminarMirror;
import webEquipeRechercheGwt.client.handler.navigation.ClickHandlerGetSeminarCreationLabel;
import webEquipeRechercheGwt.client.handler.navigation.ClickHandlerSeminar;
import webEquipeRechercheGwt.client.services.SeminarService;
import webEquipeRechercheGwt.client.services.SeminarServiceAsync;
import webEquipeRechercheGwt.client.structure.DataShowingPanel;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;

public class AllSeminarPanel extends AllEntityShowingPanel {

	private DataShowingPanel mainPanel;
	private Collection<SeminarMirror> seminars;
	
	public AllSeminarPanel(DataShowingPanel mainPanel) {
		super();
		this.mainPanel = mainPanel;
		seminars = new ArrayList<SeminarMirror>();
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		updateLabels();
		loadSeminarsFromServer();
	}
	
	private void loadSeminarsFromServer(){
		SeminarServiceAsync service = GWT.create(SeminarService.class);
		AsyncCallback<Collection<SeminarMirror>> callback = new CallBackGetAllSeminar(new CommandGetAllSeminarsImplAllSeminarPanel(this));
		service.getSeminars(callback);
	}

	public void setSeminars(Collection<SeminarMirror> persons) {
		this.seminars = persons;
		updateLabels();
	}
	
	private void addCreationLink(){
		Button b = new Button("create new Seminar");
		ClickHandler handler = new ClickHandlerGetSeminarCreationLabel(mainPanel);
		b.addClickHandler(handler);
		add(b);
	}
	
	private void updateLabels(){
		clear();
		Label title = new Label("List of all Seminars");
		add(title);
		for(SeminarMirror s : seminars){
			Label l = new Label(s.getTitle());
			ClickHandler clickHandler = new ClickHandlerSeminar(s, mainPanel);
			l.addClickHandler(clickHandler);
			add(l);
		}
		addCreationLink();
	}

}
